package kr.lim.hy.clicker.act;

import kr.lim.hy.clicker.R;
import kr.lim.hy.clicker.R.string;
import kr.lim.hy.clicker.util.EffectUtil;
import kr.lim.hy.clicker.util.StaticClass;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.Toast;

import com.google.analytics.tracking.android.EasyTracker;

public class MainActivity extends Activity implements OnClickListener {

	private Button btn_click;

	private Context context;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		context = this;

		btn_click = (Button) findViewById(R.id.btn_click);

		EffectUtil.sound_init(this);

		btn_click.setOnClickListener(this);

		VolumeMax();

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.btn_click:
			// EasyTracker.getInstance(this)
			EffectUtil.sound_play_nodelay();

			break;

		default:
			break;
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main_actions, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		Intent intent;
		switch (item.getItemId()) {

		case R.id.action_help:

			intent = new Intent(MainActivity.this, HelpActivity.class);
			startActivity(intent);

			return true;
		case R.id.action_setting:

			intent = new Intent(MainActivity.this, UserSettingActivity.class);
			startActivity(intent);

			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();

		EasyTracker.getInstance(this).activityStart(this); // Add this
															// method.
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		EasyTracker.getInstance(this).activityStop(this); // Add this
															// method.
	}

	/**
	 * 볼륨키 최대
	 */
	private void VolumeMax() {

		AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		int maxVol = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		am.setStreamVolume(AudioManager.STREAM_MUSIC, maxVol,
				AudioManager.FLAG_SHOW_UI);

		Toast.makeText(context, string.str_sound_max, Toast.LENGTH_SHORT)
				.show();

	}

	public MainActivity() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void addContentView(View view, LayoutParams params) {
		// TODO Auto-generated method stub
		super.addContentView(view, params);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		System.out.println(keyCode + event.getAction());
		SharedPreferences sharedPref = PreferenceManager
				.getDefaultSharedPreferences(this);

		if (sharedPref.getBoolean("prefSendReport", false) == true) {
			if (keyCode == 24 || keyCode == 25) {

				EffectUtil.sound_play_nodelay();
				return true;
			}
		}

		return super.onKeyDown(keyCode, event);
	}
}
